<!-- 作者:唐嘉诚 -->
<!-- 添加剧照 -->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .imgs{
            margin-left: 110px;
            margin-top: 10px;
            position: relative;
            width:150px;
            height:150px;
            display: none;
        }
        .imgs i{
            position: absolute;
            right: 2px;
            top: 2px;
            color:#fff;
            z-index:1;
            cursor: pointer;
        }
        .imgs::after{
            content: "";
            position:absolute;
            height:20px;
            background:rgba(0,0,0,.5);
            top:0;
            left:0;
            width:100%;
            border-radius: 5px 5px 0 0;

        }
        .imgs img{
            width:100%;
            height:100%;
            display: block;
            object-fit: contain;
            border-radius:5px;
            border: 1px solid gray;
            box-sizing: border-box;
        }
        .layui-layout-body{
            overflow: scroll;
        }
    </style>
</head>

<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-body" style="position:absolute;left:-10px;top:10px;">
            <div class="layui-form-item" id="filmname" style="width:200px;margin-left:20px;"></div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:34px;">剧照</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="test1">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <div class="imgs" style="margin-left:0px">
                        <img src="" alt="">
                        <i class="layui-icon layui-icon-close"></i>
                    </div>
                    <div class="img-input" style="display:none">
                        <input type="text" id="txtimg" name="cover">
                    </div>
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="movie-add">添加</button>
                </div>
            </div>
            <div class="layui-form-item">
                <table class="layui-hide" id="tabStu" lay-filter="test"></table>
            </div>
        </div>
    </div>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>

    <script id="imgtemplet" type="text/html">
        <div>
            <img src="{{d.images}}">
        </div>
    </script>

    <script>
        function query(url) {
            const parts = location.href.split("?");
            if (parts.length == 1) {
                return {};
            }
            const obj = {};
            parts[1].split("&").map(item => item.split("=")).forEach(([p, v]) => {
                obj[p] = decodeURIComponent(v);
            });
            return obj;
        }
        const q = query();
        layui.use(['element', 'table'], function () {
            const table = layui.table;
            const element = layui.element;
            table.render({
                elem: "#tabStu",
                url: "/api/Movie/" + q.id,
                parseData: function (res) { //res 即为原始返回的数据
                    let arr = [];
                    for (let i = 0; i < res.data.images.length; i++) {
                        arr.push({ images: res.data.images[i] })
                    }
                    return {
                        code: 0,
                        msg: "",
                        count: 0,
                        data: arr
                    };
                },
                page: true,
                cols: [[
                    {
                        title: "图片",
                        templet: `#imgtemplet`,
                        width: "200"
                    },
                    {
                        title: "操作",
                        templet: `<div class="layui-btn-group">
                                    <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="delStills('{{d.images}}',this)">
                                        <i class="layui-icon">&#xe640;</i>
                                    </button>
                                </div>`,
                        width: "200"
                    }
                ]]
            })
        })

        layui.use(['form', 'layer'], async function () {
            const form = layui.form;
            const movie = await $.get("/api/Movie/" + q.id);
            $("#filmname").text("电影:" + movie.data.name);
            form.on('submit(movie-add)', async function (data) {
                const images = $("#txtimg").val();
                await $.ajax({
                    type: "POST",
                    url: "/api/Movie/" + "image/" + q.id,
                    data: {
                        images
                    },
                    success: function () {
                        location.href = "SeeStills.html?id=" + q.id
                    }

                });
            });
        });

        async function delStills(url, btn) {
            console.log(q.id)
            await $.ajax({
                url: "/api/Movie/images/image/" + q.id + "?deleteUrl="+url,
                method: "delete"
            });
            $(btn).parents("tr").remove();
        }

        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#test1', //绑定元素
                url: '/api/upload', //上传接口
                field: "imgfile",
                acceptMime: "image/*",
                size: 500,
                multiple: true,
                drag: true,
                done: function (res) {
                    //上传完毕回调
                    $(".imgs img").prop("src", res.data);
                    $("#txtimg").val(res.data[0]);
                    $(".imgs").hide();
                }
                , error: function () {
                    //请求异常回调
                    console.log("错误");
                }
            });
        });
    </script>
</body>

</html>